﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using StarterKits.Mvc.Membership;
using System.Web;

namespace StarterKits.Mvc.Membership.ComponentControllers
{
	public class WindowsLiveComponentController : ComponentController
	{
		private string GetUserId( WindowsLiveLogin wll )
		{
			HttpCookie loginCookie = this.Context.HttpContext.Request.Cookies["webauthtoken"];

			if( loginCookie != null )
			{
				string token = loginCookie.Value;

				if( !string.IsNullOrEmpty( token ) )
				{
					WindowsLiveLogin.User user = wll.ProcessToken( token );

					if( user != null )
					{
						return user.Id;
					}
				}
			}

			return null;
		}

		public virtual void SignIn()
		{
			WindowsLiveLogin wll = new WindowsLiveLogin( true );

			var viewData = new SignInViewData
			{
				AppId = wll.AppId,
				UserId = GetUserId( wll )
			};

            RenderView("SignIn", viewData);
		}

		public virtual void LoginStatus()
		{
			WindowsLiveLogin wll = new WindowsLiveLogin( true );

			var viewData = new SignInViewData
			{
				AppId = wll.AppId,
				UserId = GetUserId( wll )
			};

            RenderView("LoginStatus", viewData);
		}

		public class SignInViewData
		{
			public string AppId { get; set; }
			public string UserId { get; set; }
		}
	}
}
